package com.hyxt.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hyxt.api.ChainLinkServer;
import com.hyxt.handler.TransferHandler;
import com.hyxt.utils.Caches;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author songm
 * @version v1.0
 * @Description 接收转发链路客户端消息
 * @Date: Create in 18:38 2017/12/19
 * @Modifide By:
 **/
@Service("receiveTransferClientMsg")
public class ReceiveTransferClientMsg implements IReceiveMsg{

    //交通部链路管理
    @Reference(version = "1.0.1")
    private ChainLinkServer chainLinkServer2;

    @Override
    public Boolean listen(final String channelName) {
        if (channelName != null) {
            //开启一个线程持续监听通道
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        // todo 调用链路管理监听接口
                        String message = chainLinkServer2.receiveMsg(channelName);
                        TransferHandler handler = (TransferHandler) Caches.transferHandlers.get(channelName);
                        if (handler == null){
                            continue;
                        }
                        handler.setTransferChannel(channelName);
                        handler.process(message);
                    }
                }
            }).start();
            return true;
        }
        return false;
    }
}
